//
//  DynamicAdMobTableViewCell.m
//  WeCommunity
//
//  Created by mac on 2017/7/14.
//  Copyright © 2017年 lzy. All rights reserved.
//

#import "DynamicAdMobTableViewCell.h"

@interface DynamicAdMobTableViewCell ()<GADBannerViewDelegate>

@property (nonatomic,strong) GADBannerView * m_gadBannerView;
@property (nonatomic,strong) AdMobModel * model;

@property (nonatomic,strong) UIActivityIndicatorView * m_activityIndicator;

@end

@implementation DynamicAdMobTableViewCell


-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        self.backgroundColor = WHITE_COLOR;
        
        self.m_activityIndicator = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
        [self.contentView addSubview:self.m_activityIndicator];

        CGFloat height = KScreen_Width;
        if (IS_IPAD) {
            height = KScreen_Width * 0.8;
        }
        self.m_activityIndicator.center = CGPointMake(KScreen_Width * 0.5, height * 0.5);
    }
    return self;
}

-(void)creatCellWithModel:(AdMobModel * )model root:(UIViewController *)rootViewController
{
    self.model = model;
    
    if (self.m_gadBannerView) {
        [self.m_gadBannerView removeFromSuperview];
    }
    
    self.m_gadBannerView = model.m_gadBannerView;
    [self.contentView addSubview:self.m_gadBannerView];
    
    if (!self.model.loadEnd) {
        self.m_gadBannerView.adUnitID = [GooGleAdMobMgr shared].normalAdKey;
        self.m_gadBannerView.rootViewController = rootViewController;
        self.m_gadBannerView.delegate = model;
        [self.m_gadBannerView loadRequest:[GADRequest request]];
        self.m_gadBannerView.backgroundColor = [UIColor whiteColor];
        
        model.m_activityIndicator =  self.m_activityIndicator;
        [self.contentView bringSubviewToFront:self.m_activityIndicator];
        [self.m_activityIndicator startAnimating];
    }
}


@end
